package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    RoleService roleService;

    @RequestMapping("/findAllRoles")
	public Result findAllRoles(){
		try {
			List<Role> allRoles = roleService.findAllRoles();
			return new Result(true,MessageConst.QUERY_USER_SUCCESS,allRoles);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true,MessageConst.QUERY_USER_FAIL);
		}
	}


    //分页查询获取所有角色数据
    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = roleService.findAll(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

	//向数据库中添加角色
	@RequestMapping("/add")
	public Result add(@RequestBody Map<String , Object> pages){
		//formData
		JSONObject jsonObject = (JSONObject) pages.get("formData");
		Role role = jsonObject.toJavaObject(Role.class);
		//checkitemIds
		JSONArray jsonObject1 = (JSONArray) pages.get("checkitemIds");
		Integer[] checkitemIds = jsonObject1.toArray(new Integer[]{});
		//menukitemIds
		JSONArray jsonArray = (JSONArray) pages.get("menukitemIds");
		Integer[] menukitemIds = jsonArray.toArray(new Integer[]{});
		try {
			roleService.add(role,checkitemIds,menukitemIds);
			return new Result(true,"角色添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"角色添加失败");
		}
	}

	//查询指定id的角色对象
	@RequestMapping("/findById")
	public Result findById(Integer id) {
		try {
			Role role = roleService.findById(id);
			System.out.println("role = " + role);
			return new Result(true, "查询角色成功", role);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询角色失败");
		}
	}

	//查询角色和权限的中间表数据
	@RequestMapping("/findR_P")
	public Result findR_P(Integer id) {
		try {
			Integer[] integers = roleService.findR_P(id);
			return new Result(true, "回显已选中权限成功", integers);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true, "回显已选中权限失败");
		}
	}

	//编辑角色信息
	@RequestMapping("/update")
	public Result update(@RequestBody Map<String , Object> pages){
		JSONObject jsonObject = (JSONObject) pages.get("formData");
		Role role = jsonObject.toJavaObject(Role.class);
		//checkitemIds
		JSONArray jsonObject1 = (JSONArray) pages.get("checkitemIds");
		Integer[] checkitemIds = jsonObject1.toArray(new Integer[]{});
		//menukitemIds
		JSONArray jsonArray = (JSONArray) pages.get("menukitemIds");
		Integer[] menukitemIds = jsonArray.toArray(new Integer[]{});
		try {
			roleService.update(role,checkitemIds,menukitemIds);
			return new Result(true,"编辑角色成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"编译角色失败");
		}
	}

    //删除角色
    @RequestMapping("/del")
    public Result del(Integer id){
        try {
            roleService.del(id);
            return new Result(true,"删除成功");
        }catch (RuntimeException r){
            return new Result(false,r.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

}
